package com.cheng.service.transit.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.takedelivery.WayBillDao;
import com.cheng.dao.transit.TransitinfoDao;
import com.cheng.domain.takedelivery.WayBill;
import com.cheng.domain.transit.TransitInfo;
import com.cheng.index.takedelivery.WayBillIndexDao;
import com.cheng.service.transit.TransitinfoService;
@Service
@Transactional
public class TransitinfoServiceImpl implements TransitinfoService {
	@Autowired
	private TransitinfoDao transitInfoDao;
	@Autowired
	private WayBillDao wayBillDao;
	@Autowired
	private WayBillIndexDao wayBillIndexDao;

	@Override
	public void creatTransitInfo(String wayBillIds) {
		if (StringUtils.isNotBlank(wayBillIds)) {
			for (String sid : wayBillIds.split(",")) {
				WayBill wayBill = wayBillDao.findOne(Integer.parseInt(sid));
				//运单为待发货下才能开启中转配送
				if (wayBill.getSignStatus() == 1) {
					System.out.println("生成配送单");
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");
					transitInfoDao.save(transitInfo);
					//修改运单状态
					wayBill.setSignStatus(2);
					//同步索引库
					wayBillIndexDao.save(wayBill);
				}
			}
		}
	}

	@Override
	public Page<TransitInfo> findAll(Pageable pageable) {
		return transitInfoDao.findAll(pageable);
	}

}
